package com.iss.umservice.manager.impl;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iss.umservice.bean.CompanyApplyBean;
import com.iss.umservice.bean.CompanyBean;
import com.iss.umservice.dao.CompanyDao;
import com.iss.umservice.dao.CompanyapplyDao;
import com.iss.umservice.manager.AccountManager;
import com.iss.umservice.manager.CompanyManager;
import com.iss.umservice.manager.MonthReportManager;

@Service
public class CompanyManagerImpl implements CompanyManager {
	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private CompanyDao companyDao;
	@Autowired
	private CompanyapplyDao companyApplyDao;

	@Autowired
	private AccountManager accountManager;

	@Autowired
	private MonthReportManager monthReportManager;

	@Override
	public int addCompany(CompanyBean newComp) {
		int re = companyDao.addCompany(newComp);
		accountManager.createCompAccount("" + re,newComp.getCompanyname());
		return re;
	}

	@Override
	public void modCompany(CompanyBean comp) {
		companyDao.modCompany(comp);
		Integer st = comp.getCompanystatus();
		if (st != null)
			if (st == 4 || st == 5 || st == 6) {
				monthReportManager.compBack("" + comp.getId());
			}
	}

	@Override
	public void delCompany(CompanyBean comp) {
		companyDao.delCompany(comp);
	}

	@Override
	public List<CompanyBean> queryCompanys(CompanyBean condition, int offset,
			int row) {
		return companyDao.queryCompanys(condition, offset, row);
	}

	@Override
	public CompanyBean getCompany(CompanyBean condition) {
		return companyDao.getCompany(condition);
	}

	@Override
	public List<CompanyBean> allCompany() {
		return companyDao.allCompany();
	}

	@Override
	public int countCompany(CompanyBean condition) {
		return companyDao.countCompany(condition);
	}

	@Override
	public void discardCompany(int[] ids) {
		for (int id : ids) {
			companyDao.discardCompany(id);
			monthReportManager.compBack("" + id);
		}
	}

	// ------------------------------------------------------------------------------------
	@Override
	public int countCompanyApply(CompanyApplyBean condition) {
		return companyApplyDao.countCompanyapply(condition);
	}

	@Override
	public List<com.iss.umservice.bean.CompanyApplyBean> queryCompanyapplys(
			com.iss.umservice.bean.CompanyApplyBean condition, int offset,
			int row) {
		return companyApplyDao.queryCompanyapplys(condition, offset, row);
	}

	@Override
	public int addCompanyApply(CompanyApplyBean apply) {
		return companyApplyDao.addCompanyapply(apply);
	}

	@Override
	public void modCompanyApplyToOk(int applyId) {
		CompanyApplyBean a = new CompanyApplyBean();
		a.setId(applyId);
		a = companyApplyDao.getCompanyapply(a);
		a.setApplystatus(3);
		companyApplyDao.modCompanyapply(a);
		//
		a.setId(a.getApplyupdatecompid());
		companyDao.modCompany(a);
	}

	@Override
	public void modCompanyApplyToCansel(int applyId) {
		CompanyApplyBean a = new CompanyApplyBean();
		a.setId(applyId);
		a = companyApplyDao.getCompanyapply(a);
		a.setApplystatus(4);
		companyApplyDao.modCompanyapply(a);
	}

	@Override
	public CompanyApplyBean getCompanyApply(CompanyApplyBean bean) {
		return companyApplyDao.getCompanyapply(bean);
	}
}
